package com.prs.crm.action.sys;

import java.util.ArrayList;
import java.util.Collection;
import java.util.List;

import org.apache.struts2.convention.annotation.Action;
import org.apache.struts2.convention.annotation.Result;
import org.courser.struts.BaseAction;
import org.courser.ui.PaginationSupport;
import org.courser.ui.TreeNode;
import org.hibernate.criterion.DetachedCriteria;

import com.prs.crm.domain.hr.Dept;
import com.prs.crm.domain.meeting.MeetSource;
import com.prs.crm.domain.sys.SysDepartment;
import com.prs.crm.service.sys.SysDepartmentService;

public class SysDepartmentAction extends BaseAction{

	private static final long serialVersionUID = 1L;
	private SysDepartment sysDepartment;
	private SysDepartmentService sysDepartmentService;
	private PaginationSupport pagination;
	private Integer[] ids;
	private List<SysDepartment> dept;
	private Collection<TreeNode> nodes;
	private SysDepartment parentSysDepartment;
	private String serCode;
	private TreeNode root;

	public TreeNode getRoot() {
		return root;
	}
	public void setRoot(TreeNode root) {
		this.root = root;
	}
	public String getSerCode() {
		return serCode;
	}
	public void setSerCode(String serCode) {
		this.serCode = serCode;
	}
	public Collection<TreeNode> getNodes() {
		return nodes;
	}
	public void setNodes(Collection<TreeNode> nodes) {
		this.nodes = nodes;
	}
	public SysDepartment getParentSysDepartment() {
		return parentSysDepartment;
	}
	public void setParentSysDepartment(SysDepartment parentSysDepartment) {
		this.parentSysDepartment = parentSysDepartment;
	}
	public SysDepartment getSysDepartment() {
		return sysDepartment;
	}
	public void setSysDepartment(SysDepartment sysDepartment) {
		this.sysDepartment = sysDepartment;
	}
	public SysDepartmentService getSysDepartmentService() {
		return sysDepartmentService;
	}
	public void setSysDepartmentService(SysDepartmentService sysDepartmentService) {
		this.sysDepartmentService = sysDepartmentService;
	}
	public List<SysDepartment> getDept() {
		return dept;
	}
	public void setDept(List<SysDepartment> dept) {
		this.dept = dept;
	}
	public Integer[] getIds() {
		return ids;
	}
	public void setIds(Integer[] ids) {
		this.ids = ids;
	}
	public PaginationSupport getPagination() {
		return pagination;
	}
	public void setPagination(PaginationSupport pagination) {
		this.pagination = pagination;
	}	
	
	@Action("listSysDepartment")
	public String listSysDepartment(){
		DetachedCriteria criteria=this.getPaginationHelper().createQuery(SysDepartment.class);
		pagination=this.getPaginationHelper().prepare(pagination);
		this.setPagination(this.getSysDepartmentService().listByPage(criteria, pagination));
		return SUCCESS;
	}
	
	@Action("editSysDepartment")
	public String editSysDepartment(){
		this.dept=this.sysDepartmentService.getAllSysDepartment();
		return SUCCESS;
	}
	@Action("viewSysDepartment")
	public String viewSysDepartment(){
		return SUCCESS;
	}
	@Action("selectSysDept")
	public String selectSysDept(){
		this.sysDepartment=this.getSysDepartmentService().getSysDepartment(serCode);
		return SUCCESS;
	}
	
//	@Action(value="ajaxDeptId",results = { @Result(name = "success", type = "json",
//			params = {"root", "dept" }) })
//	public String ajaxDeptId(){
//		dept=this.departmentService.getDepartmentByDeptId(deptId);
//		return SUCCESS;
//	}
//	
	@Action(value="saveSysDepartment",results={
			@Result(name="success",type="redirect",location="listSysDepartment"),
			@Result(name="error",type="chain",location="editSysDepartment")
	})
	public String saveSysDepartment(){
		SysDepartment dept=this.getSysDepartmentService().getSysDepartment("JxCompany");
		if(dept==null){
			dept=new SysDepartment();
			dept.setCompanyCode("JxCompany");
			this.getSysDepartmentService().save(dept);		
		}else{
			dept.setName(sysDepartment.getName());
			this.getSysDepartmentService().save(dept);
		}
		sysDepartment=dept;
		return SUCCESS;
	}
	@Action(value="removeSysDepartment",results={
			@Result(name="success",type="redirect",location="listSysDepartment"),
			@Result(name="error",type="chain",location="listSysDepartment")
	})
	public String removeSysDepartment(){
		if(ids!=null){
			this.sysDepartment=this.getSysDepartmentService().get(ids[0]);
		}
		if(sysDepartment.getParent()!=null){
			addActionError("数据有关联不能删除");
			return ERROR;
		}
		this.getSysDepartmentService().remove(sysDepartment);
		return SUCCESS;
	}
	
	@Action("treeSysDepartment")
	public String treeSysDepartment() {
		return SUCCESS;
	}
	
//	@Action(value = "loadSysDepartmentTree", results = { @Result(name = "success", type = "json", params = {
//			"root", "nodes" }) })
//	public String loadSysDepartmentTree() {
//		nodes=new ArrayList<TreeNode>();
//		Collection<SysDepartment> sysDepartments =this.getSysDepartmentService().getRootSysDepartment();
//		for (SysDepartment sysDepartment : sysDepartments) {
//			TreeNode node=new TreeNode();
//			node.setText(sysDepartment.getName());
//			node.setId(sysDepartment.getId().toString());
//			nodes.add(node);
//			addChildNode(sysDepartment, node);
//		}
//		return SUCCESS;
//	}
	@Action(value = "loadDeptTree", results = { @Result(name = "success", type = "json", params = {
			"root", "nodes" }) })
	public String loadDeptTree() {
		nodes = new ArrayList<TreeNode>();
		Collection<SysDepartment> sysDepartments= this.getSysDepartmentService().getRootSysDepartment();
		for (SysDepartment sysDepartment : sysDepartments) {
			TreeNode node = new TreeNode();
			node.setId(sysDepartment.getId().toString());
			node.setText(sysDepartment.getName());
			nodes.add(node);
			addChildNode(sysDepartment, node);
		}
		return SUCCESS;
	}

	private void addChildNode(SysDepartment parent, TreeNode parentNode) {
		if(parent.getChilds()!=null){
			for (SysDepartment sysDepartment : parent.getChilds()) {
				TreeNode node=new TreeNode();
				node.setText(sysDepartment.getName());
				node.setId(sysDepartment.getId().toString());
				parentNode.addChildNode(node);
				addChildNode(sysDepartment, node);
			}
		}
	}
	
	@Action(value = "saveSysDept", results = {
			@Result(name="success",type="redirect",location="listSysDepartment"),
			@Result(name="error",type="chain",location="editSysDepartment") })
	public String saveSysDept(){
		this.getSysDepartmentService().save(sysDepartment);
		return SUCCESS;
	}
	
	@Action(value = "removeSysDept", results = { @Result(name = "success", type = "json", params = {
			"root", "prompt" }) })
	public String removeSysDept() {
		try {
			this.getSysDepartmentService().remove(sysDepartment);
			return SUCCESS;
		}catch (Exception e) {
			this.setPrompt("error");
		}
			return SUCCESS;
	}
	
	@Action(value = "loadDepartTree", results = { @Result(name = "success", type = "json", params = {
			"root", "root" }) })
	public String loadDepartTree() {
		root = new TreeNode();
		root.setId("-1");

		TreeNode firstNode = new TreeNode();
		firstNode.setId("0");
		firstNode.setText("部门选择");

		Collection<SysDepartment> sysDepartments= this.getSysDepartmentService().getRootSysDepartment();

		for (SysDepartment sysDepartment : sysDepartments) {
			TreeNode node = new TreeNode();
			node.setText(sysDepartment.getName());
			node.setId("depart-" + sysDepartment.getId().toString());
			firstNode.addChildNode(node);
			addDepartNode(node, sysDepartment);
			
		}
		root.addChildNode(firstNode);
		return SUCCESS;
	}
	private void addDepartNode(TreeNode parent, SysDepartment sysDepartment) {
		for (SysDepartment depart : sysDepartment.getChilds()) {
			TreeNode node = new TreeNode();
			node.setText(depart.getName());
			node.setId("sysDepartment-" + depart.getId().toString());
			parent.addChildNode(node);
			addDepartNode(node, depart);	
		}
	}
	
}
